<?php
// +---------------------------------------------------------------------+
// | ALINEJUN    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/ALINEJUN                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use think\Db;

/**
 * 数据逻辑
 */
class Article extends LogicBase
{
    
    /**
     * 数据分类编辑
     */
    public function articleCategoryEdit($data = [])
    {
        
        $validate_result = $this->validateArticleCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateArticleCategory->getError()];
        }
        
        $url = url('articleCategoryList');
        
        $result = $this->modelArticleCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '数据分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelArticleCategory->getError()];
    }
    
    /**
     * 获取数据列表
     */
    public function getArticleList($where = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {
        
        $this->modelArticle->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelArticle->join = $join;
        
        return $this->modelArticle->getList($where, $field, $order);
    }
    
    /**
     * 获取数据列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        if(!empty($data['field'])){
            !empty($data['search_data']) && $where[$data['field']] = ['like', '%'.$data['search_data'].'%'];
        }
        
        return $where;
    }
    
    /**
     * 数据信息编辑
     */
    public function articleEdit($data = [])
    {
        
        $validate_result = $this->validateArticle->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateArticle->getError()];
        }
        
        $url = url('articleList');
        
        empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
        //$data['content'] = html_entity_decode($data['content']);
        
        $result = $this->modelArticle->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '数据' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '数据操作成功', $url] : [RESULT_ERROR, $this->modelArticle->getError()];
    }

    /**
     * 获取数据信息
     */
    public function getArticleInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {
        
        $this->modelArticle->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelArticle->join = $join;
        $info =  $this->modelArticle->getInfo($where, $field);
        /*if(!empty($info['province'])){
            $info['province'] = $this->logicRegion->getRegionList(['name' => $info['province'],'upid'=>0, 'level' => 1])->toArray()[0]['id'];
        }

        if(!empty($info['city'])){
            $city =  $this->logicRegion->getRegionList(['name' => $info['city'],'upid'=>$info['province'] ,'level' => 2])->toArray();
            $info['city'] = $city?$city[0]['id']:0;
        }
        if(!empty($info['county'])){
            $conty =$this->logicRegion->getRegionList(['name' => $info['county'],'upid'=>$info['city'], 'level' => 3])->toArray() ;
            $info['county'] = $conty?$conty[0]['id']:0;
        }*/

        return $info;
    }
    
    /**
     * 获取分类信息
     */
    public function getArticleCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelArticleCategory->getInfo($where, $field);
    }
    
    /**
     * 获取数据分类列表
     */
    public function getArticleCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelArticleCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 数据分类删除
     */
    public function articleCategoryDel($where = [])
    {
        
        $result = $this->modelArticleCategory->deleteInfo($where);
        
        $result && action_log('删除', '数据分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '数据分类删除成功'] : [RESULT_ERROR, $this->modelArticleCategory->getError()];
    }
    
    /**
     * 数据删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelArticle->deleteInfo($where);
        
        $result && action_log('删除', '数据删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '数据删除成功'] : [RESULT_ERROR, $this->modelArticle->getError()];
    }
}
